﻿/**
 * Created by Maibenben on 2017/5/3.
 */
$(function () {
    //跳转到本页之后读取cookie中是否有mid和token
    checkCookie();
    var mid = $.cookie("mid");
    var token = $.cookie("token");
    localStorage.removeItem("tel");
    //
    $(".add").on("click", function () {
        window.location.href = "add-person.html";
    });
    //进入页面发送请求，显示最新的数据
    $.ajax({
        url: hosURL + "?mid=" + mid + "&token=" + token,
        type: "get",
        success: function (data) {
            if (data.total == 0) {
                $("#main")
                    .html("暂无就诊人员列表")
                    .css({textAlign:"center",fontSize:"0.8rem",marginTop:"40%"});
            } else {
                $(".top").css("display", "block");
                var hosHTML = template("hosTem", data);
                $(".center").html(hosHTML);
                //控制显示绑定卡号按钮
                $(".binding").each(function () {
                    //if ($(this).attr("data-card") != "") {
                    //    $(this).css("display", "none");
                    //} else {
                    //    $(this).css("display", "block");
                    //}
                    //点击绑定跳转到绑定就诊卡页面
                    $(this).on("click", function () {
                        window.location.href = "bind-card.html?id=" + $(this).attr("data-id");
                        localStorage.setItem("tel", $(this).attr("data-tel"));
                    });
                });
                //右上角添加按钮,点击之后跳转到指定的添加页面
                if (data.av_add == 1) {
                    $(".add").on("click", function () {
                        window.location.href = "add-person.html";
                    });
                } else {
                    $(".add").on("click", function () {
                        creatLayout("就诊人最多只可以添加六个");
                    });
                }
                //每一个选项后面的删除功能
                $(".del").on("click", function () {
                    var id = $(this).attr("data-id");
                    layer.confirm('确定删除该就诊人？', {
                        btn: ['是','否'] //按钮
                    }, function(){
                        $(".del").off("click");
                        //将用户删除的选项数据从后台数据中删除
                        $.get(delMemURL + '?id=' + id + "&mid=" + mid + "&token=" + token, function (res) {
                            //删除之后刷新页面显示最新的数据
                            // if (res.state == 1) {
                            //     creatLayout("删除就诊人成功");
                            //     setTimeout(function () {
                            //         location.reload();
                            //     }, 200)
                            // } else {
                            //     creatLayout("删除就诊人失败");
                            //     location.reload();
                            // }
                        });
                    });
                });
                //挂失弹框
                var lostback = true;
                var whichlost = null;
                $(".getlost").on("click", function () {
                    whichlost = $(this);
                    $(".losttip").show();
                    $(".yes").attr("data-card", $(this).attr("data-card"));
                });
                $(".yes").on("click", function () {
                    if (lostback) {
                        lostback = false;
                        //发送挂失请求
                        $.post(lostURL, {mid: mid, token: token, card_no: $(this).attr("data-card")}, function (res) {
                            lostback = true;
                            $(".losttip").hide();
                            if (res.result == 0) {
                                creatLayout(res.errmsg);
                            } else {
                                creatLayout("挂失成功！");
                                whichlost[0].className = "losted";
                                whichlost.text("已挂失");
                            }
                            whichlost = null;
                        });
                    } else {
                        creatLayout("请稍等");
                    }
                });
                $(".no").on("click", function () {
                    $(".losttip").hide();
                });
            }
        }
    });
});
